import 'dart:convert';

import 'package:flutter_book/util/string_util.dart';

import 'book_info_rule.dart';
import 'content_rule.dart';
import 'explore_rule.dart';
import 'search_rule.dart';
import 'toc_rule.dart';

class YckRuleBook3Model {
  static const BOOK_TYPE_TEXT = "0";
  static const BOOK_TYPE_AUDIO = "1";
  String bookSourceName = ""; // 名称
  String? bookSourceGroup; // 分组
  String bookSourceUrl = ""; // 地址，包括 http/https
  String bookSourceType = BOOK_TYPE_TEXT; // 类型，0 文本，1 音频
  String? bookUrlPattern; // 详情页url正则
  int? customOrder = 0; // 手动排序编号
  bool? enabled = true; // 是否启用
  bool? enabledExplore = true; // 启用发现
  String? header; // 请求头
  String? loginUrl; // 登录地址
  String? bookSourceComment; // 注释
  int? lastUpdateTime = 0; // 最后更新时间，用于排序
  int? weight = 0; // 智能排序的权重
  String? exploreUrl; // 发现url
  ExploreRule? ruleExplore; // 发现规则
  String? searchUrl; // 搜索url
  SearchRule? ruleSearch; // 搜索规则
  BookInfoRule? ruleBookInfo; // 书籍信息页规则
  TocRule? ruleToc; // 目录页规则
  ContentRule? ruleContent;
  String? id;
  int? addTime;

  YckRuleBook3Model(
      this.bookSourceName,
      this.bookSourceGroup,
      this.bookSourceUrl,
      this.bookSourceType,
      this.bookUrlPattern,
      this.customOrder,
      this.enabled,
      this.enabledExplore,
      this.header,
      this.loginUrl,
      this.bookSourceComment,
      this.lastUpdateTime,
      this.weight,
      this.exploreUrl,
      this.ruleExplore,
      String? searchUrl,
      this.ruleSearch,
      this.ruleBookInfo,
      this.ruleToc,
      this.ruleContent,
      this.id,
      this.addTime) {
    if (searchUrl != null) {
      this.searchUrl = searchUrl.replaceAll(bookSourceUrl, "");
    }
  }

  @override
  int get hashCode {
    return bookSourceUrl.hashCode;
  }

  @override
  bool operator ==(Object other) {
    if (other is YckRuleBook3Model) {
      return other.bookSourceUrl == bookSourceUrl;
    } else {
      return false;
    }
  }

  Map<String, dynamic> toJson() {
    var map = <String, dynamic>{};
    map['bookSourceName'] = bookSourceName;
    map['bookSourceGroup'] = bookSourceGroup;
    map['bookSourceUrl'] = bookSourceUrl;
    map['bookSourceType'] = bookSourceType;
    map['bookUrlPattern'] = bookUrlPattern;
    map['customOrder'] = customOrder;
    map['enabled'] = enabled;
    map['enabledExplore'] = enabledExplore;
    map['header'] = header;
    map['loginUrl'] = loginUrl;
    map['bookSourceComment'] = bookSourceComment;
    map['lastUpdateTime'] = lastUpdateTime;
    map['weight'] = weight;
    map['exploreUrl'] = exploreUrl;
    map['ruleExplore'] = ruleExplore;
    map['searchUrl'] = searchUrl;
    map['ruleSearch'] = ruleSearch;
    map['ruleBookInfo'] = ruleBookInfo;
    map['ruleToc'] = ruleToc;
    map['ruleContent'] = ruleContent;
    map['id'] = id;
    map['addTime'] = addTime;
    return map;
  }

  YckRuleBook3Model.fromJson(dynamic jsonMap, [String? id]) {
    if (jsonMap != null) {
      bookSourceName = jsonMap['bookSourceName'];
      bookSourceGroup = jsonMap['bookSourceGroup'];
      bookSourceUrl = jsonMap['bookSourceUrl'];
      if (jsonMap['bookSourceType'] != null && jsonMap['bookSourceType'] is int) {
        if (jsonMap['bookSourceType'] == 0) {
          bookSourceType = BOOK_TYPE_TEXT;
        } else {
          bookSourceType = BOOK_TYPE_AUDIO;
        }
      } else if (jsonMap['bookSourceType'] != null && jsonMap['bookSourceType'] is String) {
        if (jsonMap['bookSourceType'] == "0") {
          bookSourceType = BOOK_TYPE_TEXT;
        } else {
          bookSourceType = BOOK_TYPE_AUDIO;
        }
      }
      bookUrlPattern = jsonMap['bookUrlPattern'];
      customOrder = jsonMap['customOrder'];
      enabled = jsonMap['enabled'];
      enabledExplore = jsonMap['enabledExplore'];
      header = jsonMap['header'];
      loginUrl = jsonMap['loginUrl'];
      bookSourceComment = jsonMap['bookSourceComment'];
      lastUpdateTime = jsonMap['lastUpdateTime'];
      weight = jsonMap['weight'];
      exploreUrl = jsonMap['exploreUrl'];

      if (jsonMap['ruleExplore'] is Map<String, dynamic>) {
        ruleExplore = ExploreRule.fromJson(jsonMap['ruleExplore']);
      } else if (jsonMap['ruleExplore'] == null || jsonMap['ruleExplore'] is String) {
        ruleExplore = ExploreRule.fromJson(json.decode(jsonMap['ruleExplore'] ?? ""));
      } else if (jsonMap['ruleExplore'] is ExploreRule) {
        ruleExplore = jsonMap['ruleExplore'];
      } else {
        ruleExplore = ExploreRule.fromJson(json.decode(""));
      }

      if (jsonMap["searchUrl"] != null) {
        searchUrl = jsonMap["searchUrl"].toString().replaceAll(bookSourceUrl, "");
      }

      if (jsonMap['ruleSearch'] is Map<String, dynamic>) {
        ruleSearch = SearchRule.fromJson(jsonMap['ruleSearch']);
      } else if (jsonMap['ruleSearch'] == null || jsonMap['ruleSearch'] is String) {
        ruleSearch = SearchRule.fromJson(json.decode(jsonMap['ruleSearch'] ?? ""));
      } else if (jsonMap['ruleSearch'] is SearchRule) {
        ruleSearch = jsonMap['ruleSearch'];
      } else {
        ruleSearch = SearchRule.fromJson(json.decode(""));
      }

      if (jsonMap['ruleBookInfo'] is Map<String, dynamic>) {
        ruleBookInfo = BookInfoRule.fromJson(jsonMap['ruleBookInfo']);
      } else if (jsonMap['ruleBookInfo'] == null || jsonMap['ruleBookInfo'] is String) {
        ruleBookInfo = BookInfoRule.fromJson(json.decode(jsonMap['ruleBookInfo'] ?? ""));
      } else if (jsonMap['ruleBookInfo'] is BookInfoRule) {
        ruleBookInfo = jsonMap['ruleBookInfo'];
      } else {
        ruleBookInfo = BookInfoRule.fromJson(json.decode(""));
      }

      if (jsonMap['ruleToc'] is Map<String, dynamic>) {
        ruleToc = TocRule.fromJson(jsonMap['ruleToc']);
      } else if (jsonMap['ruleToc'] == null || jsonMap['ruleToc'] is String) {
        ruleToc = TocRule.fromJson(json.decode(jsonMap['ruleToc'] ?? ""));
      } else if (jsonMap['ruleToc'] is TocRule) {
        ruleToc = jsonMap['ruleToc'];
      } else {
        ruleToc = TocRule.fromJson(json.decode(""));
      }

      if (jsonMap['ruleContent'] is Map<String, dynamic>) {
        ruleContent = ContentRule.fromJson(jsonMap['ruleContent']);
      } else if (jsonMap['ruleContent'] == null || jsonMap['ruleContent'] is String) {
        ruleContent = ContentRule.fromJson(json.decode(jsonMap['ruleContent'] ?? ""));
      } else if (jsonMap['ruleContent'] is ContentRule) {
        ruleContent = jsonMap['ruleContent'];
      } else {
        ruleContent = ContentRule.fromJson(json.decode(""));
      }

      if (jsonMap["addTime"] != null) {
        addTime = jsonMap["addTime"];
      } else {
        addTime = DateTime.now().millisecondsSinceEpoch;
      }

      if (id != null || jsonMap["id"] != null) {
        this.id = id ?? jsonMap["id"];
      } else {
        this.id = StringUtil.getMd5(bookSourceUrl) + StringUtil.getMd5(bookSourceName) + addTime.toString();
      }
    }
  }
}
